package com.yuzhi.common.jackson;

import com.fasterxml.jackson.databind.BeanDescription;
import com.fasterxml.jackson.databind.SerializationConfig;
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
import com.fasterxml.jackson.databind.ser.BeanSerializerModifier;
import com.yuzhi.common.annotation.DictFormat;

import java.util.List;

/**
 * @ClassName DictSerializerModifier
 * @Author Mario
 * @Description: 字典翻译序列化修饰符
 */
public class DictFormatSerializerModifier extends BeanSerializerModifier {
    @Override
    public List<BeanPropertyWriter> changeProperties(SerializationConfig config, BeanDescription beanDesc, List<BeanPropertyWriter> beanProperties) {
        for (BeanPropertyWriter writer : beanProperties) {
            //查找是否有{@link DictTran}注解，有就更改序列化器
            DictFormat dictTranFormat = writer.getAnnotation(DictFormat.class);
            if (dictTranFormat != null) {
                //更改序列化器
                DictTranCodeSerializer dictTranCodeSerializer = new DictTranCodeSerializer();
                dictTranCodeSerializer.setDictTranFormat(dictTranFormat);
                writer.assignSerializer(dictTranCodeSerializer);
            }
        }
        return beanProperties;
    }
}
